
package org.openeai.lite.console.actionhandlers;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.openeai.lite.common.control.ESBController;
import org.openeai.lite.common.exception.OpenEAIException;
import org.openeai.lite.common.util.Constants;


@ManagedBean(name = "esbStopStartActionHandler")
@RequestScoped
public class ESBStopStartActionHandler
{

    private static Logger log = Logger.getLogger(ESBStopStartActionHandler.class);

    // properties
    private String status;

    private String message;

    public String getStatus()
    {
        if (ESBController.getInstance().isStarted())
        {
            this.status = Constants.ACTIVE;
        }
        else
        {
            this.status = Constants.INACTIVE;
        }
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }

    public String getMessage()
    {
        return this.message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    // Start the ESB
    public void startESB() throws OpenEAIException
    {
        log.info("Starting ESB...");
        ESBController.getInstance().start(null, null);

        if (ESBController.getInstance().isStarted())
        {
            setStatus(Constants.ACTIVE);
            setMessage("ESB started.");
        }
        else
        {
            setStatus(Constants.INACTIVE);
            setMessage("Error starting ESB!");
        }

    }

    // Stop the ESB
    public void stopESB() throws OpenEAIException
    {
        log.info("Stopping ESB...");
        // ESBController.getInstance().classicStop();
        ESBController.getInstance().stop();

        if (ESBController.getInstance().isStarted())
        {
            setStatus(Constants.ACTIVE);
            setMessage("Error stopping ESB!");
        }
        else
        {
            setStatus(Constants.INACTIVE);
            setMessage("ESB stopped.");
        }
    }
}
